package cn.lyjuan.android.demofive.frame.tpl;

import android.support.annotation.Nullable;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chad on 13/11/2017.
 */
public abstract class AbstractListViewModel<T, E> implements IListViewModel<T, E>
{
    /**
     * 数据源
     */
    protected IDataSource<E> dataSource;

    /**
     * 控件
     */
    protected View listView;
    /**
     * 数据
     */
    protected List<T> list = new ArrayList<>();

    /**
     * 数据-视图 适配器
     */
    protected Object adapter;

    public AbstractListViewModel(View listView)
    {
        this.listView = listView;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public T getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public void onFetchFail(int errType, @Nullable E data)
    {
        // 空实现，如果有需要可心实现
    }

    @Override
    public void onItemClick(int position)
    {
        // 空实现，如果有需要可心实现
    }

    @Override
    public void onItemLongClick(int position)
    {
        // 空实现，如果有需要可以实现
    }

    @Override
    public void loadData(boolean clear)
    {
        if (clear) list.clear();

        if (null == dataSource)
            loadDataSource();

        dataSource.fetchData(clear);
    }

    @Override
    public void setAdapter(Object adapter)
    {
        this.adapter = adapter;
    }
}
